﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using TA.Turbo;
using TA.Turbo.Controls;

public partial class SimpleForm_Default : TPage
{
	protected override void OnLoad(EventArgs e)
	{
		base.OnLoad(e);

		if (this.IsPostBack)
		{
			switch (this.LoadIteration)
			{
				case 0:
					if (this.EventTarget != this.rblType)
					{
						this.UpdateControl();
						this.LoadAgain = true;
					}
					break;
			}
		}
	}
	protected override void OnUnload(EventArgs e)
	{
		base.OnUnload(e);

		this.upd.Controls.Clear();
		this.upd2.Controls.Clear();
	}
	protected void rblType_ValueChanged(object s, EventArgs e)
	{
		UpdateControl();
	}

	private void UpdateControl()
	{
		this.upd.Controls.Clear();
		switch (this.Request.Form[this.rblType.UniqueID])
		{
			case "Drop down list":
				PostScenario psDdlValueChanged = new TemporaryPostScenario
				{
					ID = "psDdlValueChanged",
					UpdatePanelIds = "upd2",
					PostControlIds = "rblType, ddl", // send rblType over again, so that we know which control to generate next time
				};

				DropDownList ddl = new DropDownList
				{
					ID = "ddl",
				};
				ddl.ValueChanged.Server += this.ddl_ValueChanged;
				ddl.ValueChanged.PostScenario = psDdlValueChanged;
				ddl.Items.Add(new ListItem { Text = "Item 1" });
				ddl.Items.Add(new ListItem { Text = "Item 2" });
				upd.Controls.Add(ddl);
				break;
			case "Text box":
				PostScenario psTxtValueChanged = new TemporaryPostScenario
				{
					ID = "psTxtValueChanged",
					UpdatePanelIds = "upd2",
					PostControlIds = "rblType,txt",// send rblType over again, so that we know which control to generate next time
				};

				TextBox txt = new TextBox
				{
					ID = "txt",
				};
				txt.ValueChanged.Server += this.txt_ValueChanged;
				txt.ValueChanged.PostScenario = psTxtValueChanged;
				upd.Controls.Add(txt);
				break;
		}
	}
	protected void ddl_ValueChanged(object s, EventArgs e)
	{
		upd2.Controls.Clear();
		upd2.Controls.Add(new Label
		{
			Text = ((DropDownList)s).Value
		});
	}
	protected void txt_ValueChanged(object s, EventArgs e)
	{
		upd2.Controls.Clear();
		upd2.Controls.Add(new Label
		{
			Text = ((TextBox)s).Value
		});
	}
}
